<template>
  <div>
    <el-row class="app-toptool" type="flex">
      <el-col :span="16">
        <Search size="mini" :search-visible="searchVisible" :search-data.sync="searchData" :search-form="searchForm"
          @refesh_list="searchgo" />
      </el-col>
      <el-col :span="8">
        <div class="btn-group" style="margin-bottom:11px;float: right;">
          <div>
            <el-button v-if="checkPermission('/order/serviceadd')" type="primary" size="mini" icon="el-icon-plus"
              @click="add()">添加</el-button>
            <el-button
              v-if="checkPermission('/order/servicedumpdata')"
              type="primary"
              size="mini"
              icon="el-icon-download"
              @click="dumpdata(ids)"
            >导出</el-button>
            <el-button v-if="checkPermission('/order/servicedelete')" type="primary" :disabled="multiple" size="mini"
              icon="el-icon-delete" @click="del(ids)">删除
            </el-button>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-tabs type="card" @tab-click="handleClick">
      <el-tab-pane :label="'待付款('+ countdata.statuscount1+')'" name="1" />
      <el-tab-pane :label="'待指派('+ countdata.statuscount2+')'" name="2" />
      <el-tab-pane :label="'待服务('+ countdata.statuscount3+')'" name="3" />
      <el-tab-pane :label="'服务中('+ countdata.statuscount8+')'" name="8" />
      <el-tab-pane :label="'已取消('+ countdata.statuscount4+')'" name="4" />
      <el-tab-pane :label="'已服务('+ countdata.statuscount5+')'" name="5" />
      <el-tab-pane :label="'售后('+ countdata.statuscount6+')'" name="6" />
      <el-tab-pane :label="'已完成('+ countdata.statuscount7+')'" name="7" />
    </el-tabs>
    <div>
      <el-table ref="multipleTable" v-loading="loading" :row-class-name="rowClass" row-key="id"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }" :border="false" :stripe="true" class="eltable"
        :data="list" style="width: 100%" @selection-change="selection">
        <el-table-column align="center" type="selection" width="42" />
        <el-table-column align="left" property="order_num_alias" label="订单号" width="80" />
        <el-table-column align="left" label="派单信息" width="130">
          <template slot-scope="scope">
            <div v-if="scope.row.sid">门店：{{scope.row.sid}}</div>
            <div v-if="scope.row.staff">{{lang.technical}}：{{scope.row.staff}}</div>
          </template>
        </el-table-column>
        <el-table-column align="left" label="用户信息" width="280">
          <template slot-scope="scope">
            <div v-if="scope.row.member">下单用户：{{scope.row.member.nickname}}</div>
            <div>联系人：{{scope.row.shipping_name}}</div>
            <div>电  话：{{scope.row.shipping_tel}}</div>
            <div>地  址：{{scope.row.shipping_province_name}}{{scope.row.shipping_city_name}}{{scope.row.shipping_district_name}}{{scope.row.shipping_address}}</div>
          </template>
        </el-table-column>
        <el-table-column label="订购服务" align="left" width="280">
          <template v-if="scope.row.orderGoods.length>0" slot-scope="scope">
            <div v-for="(item, index) in scope.row.orderGoods" :key="index">
              <div class="image__preview">
                <el-image v-if="item.image" class="table_list_pic" :src="item.image" :preview-src-list="[item.image]" />
              </div>
              <div class="goodstext">
                <div>{{ item.name+'\n' }}</div>
                <div v-if="item.sku">{{ item.sku+'\n' }}</div>
                <div v-if="item.customtext">{{ item.customtext+'\n' }}</div>
                <div v-if="scope.row.is_times==1">次卡：{{scope.row.timesmum}}次,剩余：{{scope.row.remain}}次</div>
                <div v-else>数量：{{ item.quantity }}</div>
                <div v-if="item.total">小计：{{ item.total }}</div>
              </div>
            </div>
          </template>
          <template v-else>
            <div> {{ scope.row.pay_subject }}</div>
          </template>
        </el-table-column>
        <el-table-column align="left" label="预约时间" width="108">
          <template slot-scope="scope">
            <div v-html="scope.row.yue_time"></div>
          </template>
        </el-table-column>
        <el-table-column align="left" label="服务时间" width="122">
          <template slot-scope="scope">
            <div v-if="scope.row.service_time">服务时间：</div>
            <div v-html="scope.row.service_time"></div>
            <div v-if="scope.row.complete_time">完成时间：<br>{{scope.row.complete_time}}</div>
          </template>
        </el-table-column>
        <el-table-column align="left" property="total" label="订单金额" show-overflow-tooltip width="100" />
        <el-table-column align="left" property="order_status" label="订单状态" show-overflow-tooltip width="80" />
        <el-table-column align="left" label="分成结算" width="130">
          <template slot-scope="scope">
            <div v-if="scope.row.Operatingcity_1>0">省代理：{{scope.row.Operatingcity_1}}</div>
            <div v-if="scope.row.Operatingcity_2>0">市代理：{{scope.row.Operatingcity_2}}</div>
            <div v-if="scope.row.Operatingcity_3>0">区/县代理：{{scope.row.Operatingcity_3}}</div>
            <div v-if="scope.row.TuanzhangIncome>0">社区：{{scope.row.TuanzhangIncome}}</div>
            <div v-if="scope.row.TechnicalIncome>0">师傅：{{scope.row.TechnicalIncome}}</div>
            <div v-if="scope.row.StoreIncome>0">商家：{{scope.row.StoreIncome}}</div>
          </template>
        </el-table-column>
        <el-table-column align="left" property="pay_time" label="支付时间" width="90" />
        <el-table-column align="left" property="create_time" label="下单时间" width="90" />
        <el-table-column label="支付方式" align="left">
          <template slot-scope="scope">
            <div>{{ scope.row.payment_code_name }}</div>
            <div v-if="scope.row.payment_code=='offline_pay'">
              <el-button v-if="scope.row.order_status_id==1" size="mini" type="primary" @click="offline(scope.row)">
                <i class="el-icon-s-check" />支付凭证
              </el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column :fixed="$store.getters.device !== 'mobile'?'right':false" label="操作" align="center"
          width="200">
          <template slot-scope="scope">
            <div v-if="scope.row.id">
              <el-button v-if="(scope.row.order_status_id==2) && checkPermission('/order/staff')" size="mini"
                type="primary" @click="staff(scope.row)">
                <i class="el-icon-connection" />派单
              </el-button>
              <el-button v-if="(scope.row.order_status_id==3) && checkPermission('/order/staff')" size="mini"
                type="primary" @click="staff(scope.row)">
                <i class="el-icon-connection" />重新派单
              </el-button>
              <el-button v-if="(scope.row.order_status_id==5) && checkPermission('/service/delivery')" size="mini"
                type="primary" @click="delivery(scope.row)">
                <i class="el-icon-connection" />手动结算
              </el-button>
              <el-button v-if="checkPermission('/order/servicedetail')" size="mini" type="primary"
                @click="detail(scope.row)">
                <i class="el-icon-view" />详情
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination :total="page_data.total" :page.sync="page_data.page" :limit.sync="page_data.limit"
        @pagination="index" />
    </div>
    <!--查看详情-->
    <Detail :info="detailInfo" :show.sync="dialog.detailDialogStatus" size="mini" @refesh_list="index" />
    <!--派单-->
    <Staff :info="staffInfo" :show.sync="dialog.staffDialogStatus" size="mini" @refesh_list="index" />
    <!--确认下线收款-->
    <Offline :info="offlineInfo" :show.sync="dialog.offlineDialogStatus" size="mini" @refesh_list="index" />
    <!--添加/修改-->
    <Add :info="updateInfo" :opentype="opentype" :show.sync="dialog.addDialogStatus" size="mini"
      @refesh_list="index" />
    <el-dialog title="导出进度条" :visible="dumpshow" :before-close="closedialog" width="500px">
      <el-progress :percentage="percentage" />
    </el-dialog>
  </div>
</template>
<script>
  import Search from '@/components/common/Search'
  import Pagination from '@/components/Pagination'
  import Detail from '@/views/order/servicedetail.vue'
  import Paydetail from '@/views/order/paydetail.vue'
  import Add from '@/views/order/serviceadd.vue'
  import Offline from '@/views/order/offline.vue'
  import Staff from '@/views/order/staff.vue'
  import {
    confirm,
    param2Obj
  } from '@/utils/common'
  import XLSX from 'xlsx'
  export default {
    name: 'Orderservice',
    components: {
      Search,
      Detail,
      Paydetail,
      Add,
      Staff,
      Offline,
      Pagination
    },
    data() {
      return {
        lang: {},
        dialog: {
          detailDialogStatus: false,
          paydetailDialogStatus: false,
          staffDialogStatus: false,
          addDialogStatus: false,
          offlineDialogStatus: false
        },
        ids: [],
        single: true,
        multiple: true,
        list: [],
        countdata: {
          statuscount1: 0,
          statuscount2: 0,
          statuscount3: 0,
          statuscount4: 0,
          statuscount5: 0,
          statuscount6: 0
        },
        detailInfo: {},
        staffInfo: {},
        offlineInfo: {},
        loading: false,
        opentype: 'add',
        updateInfo: {},
        dumpshow: false,
        exceldata: [],
        dumppage: 1,
        percentage: 0,
        ws: {},
        filename: '',
        page_data: {
          limit: 20,
          page: 1,
          total: 20
        },
        searchVisible: true,
        searchForm: [],
        searchData: {}
      }
    },
    mounted() {
      this.$api.post('/Lang/getlang').then(res => {
        this.lang = res.data
      })
      this.index()
      this.searchForm = [{
          type: 'Input',
          label: '订单号/客户姓名/手机号',
          prop: 'keyword',
          width: '230px'
        },
        {
          type: 'datetime',
          label: '下单时间',
          prop: 'create_time',
          width: '150px'
        }
      ]
    },
    methods: {
      searchgo() {
        this.page_data.page = 1;
        this.index()
      },
      index() {
        const param = {
          deliverymode:1,
          path: this.$route.path,
          limit: this.page_data.limit,
          page: this.page_data.page
        }
        Object.assign(param, this.searchData)
        Object.assign(param, param2Obj(this.$route.fullPath))
        this.loading = true

        this.$api.post('/order/index', param).then(res => {
          this.countdata = res.countdata
          this.list = res.data.data
          this.page_data.total = res.data.total
          this.loading = false
        })
      },
      handleClick(tab, event) {
        this.searchData.status = tab.name
        this.index()
      },
      listUpdate(row, field) {
        if (row.id) {
          this.$api.post('/order/listUpdate', {
            id: row.id,
            [field]: row[field]
          }).then(res => {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
          })
        }
      },
      add() {
        this.opentype = 'add'
        this.dialog.addDialogStatus = true
      },
      update(row) {
        this.opentype = 'update'
        const id = row.id ? row.id : this.ids.join(',')
        this.$api.post('/order/getInfo', {
          id: id
        }).then(res => {
          this.dialog.addDialogStatus = true
          this.updateInfo = res.data
        })
      },
      detail(row) {
        const id = row.id ? row.id : this.ids.join(',')
        this.$api.post('/order/getInfo', {
          id: id
        }).then(res => {
          this.dialog.detailDialogStatus = true
          this.detailInfo = res.data
        })
      },
      daydetail(row) {
        const id = row.id ? row.id : this.ids.join(',')
        this.$api.post('/order/getInfo', {
          id: id
        }).then(res => {
          this.dialog.paydetailDialogStatus = true
          this.detailInfo = res.data
        })
      },
      staff(row) {
        const id = row.id ? row.id : this.ids.join(',')
        this.$api.post('/order/getInfo', {
          id: id
        }).then(res => {
          this.dialog.staffDialogStatus = true
          this.staffInfo = res.data
        })
      },
      delivery(row) {
        confirm({
          content: '确定要结算吗'
        }).then(() => {
          const id = row.id ? row.id : this.ids.join(',')

          this.$api.post('/order/delivery', {
            id: id
          }).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.index()
          })
        }).catch(() => {})
      },
      offline(row) {
        const id = row.id ? row.id : this.ids.join(',')
        this.$api.post('/order/getInfo', {
          id: id
        }).then(res => {
          this.dialog.offlineDialogStatus = true
          this.offlineInfo = res.data
        })
      },
      del(row) {
        confirm({
          content: '确定要操作吗'
        }).then(() => {
          const ids = row.id ? row.id : this.ids.join(',')

          this.$api.post('/order/delete', {
            id: ids
          }).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.index()
          })
        }).catch(() => {})
      },
      dumpdata() {
        confirm({
          content: '确定要操作吗'
        }).then(() => {
          this.dumpshow = true
          this.confirmdumpdata()
        }).catch(() => {})
      },
      confirmdumpdata() {
        let param = {
          path: this.$route.path,
          deliverymode:1,
          page: this.dumppage
        }
        if (this.searchData) param = Object.assign(param, this.searchData)
        this.$api.post('/order/dumpdata', param).then(res => {
          //console.log(res.data);
          console.log(res.data.length);
           console.log('asdfasdfasdfasdfasdf');
          if (res.data.length > 0) {
            if (this.dumppage == 1) {
              this.exceldata.push(res.header)
            }
            res.data.forEach((item) => {
              this.exceldata.push(Object.values(item))
            })
            this.percentage = res.percentage
            this.filename = res.filename
            this.ws = XLSX.utils.aoa_to_sheet(this.exceldata)
            this.dumppage = this.dumppage + 1
            this.confirmdumpdata()
          } else {
            const wb = XLSX.utils.book_new()
            var wscols = [
                {wch:16},
                {wch:50},
                {wch:10},
                {wch:12},
                {wch:12},
                {wch:18},
                {wch:10},
                {wch:12},
                {wch:30},
                {wch:10},
                {wch:40},
                {wch:8},
                {wch:10},
                {wch:16}
            ];
           this.ws['!cols'] = wscols;

            XLSX.utils.book_append_sheet(wb, this.ws, '资料')
            XLSX.writeFile(wb, this.filename)
            this.dumpshow = false
          }
        })
      },
      closedialog() {
        this.dumpshow = false
      },
      selection(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      rowClass({
        row,
        rowIndex
      }) {
        for (let i = 0; i < this.ids.length; i++) {
          if (row.id === this.ids[i]) {
            return 'rowLight'
          }
        }
      }
    }
  }
</script>
<style scoped>
  .image__preview {
    float: left;
  }

  .goodstext {
    float: left;
    padding-left: 10px;
    width: 180px;
  }

  .table_list_pic {
    width: 60px;
    height: 60px;
    vertical-align: middle;
  }
</style>
